package com.ljx.funchat.chat.service.strategy;

import cn.hutool.core.collection.CollectionUtil;
import com.ljx.funchat.chat.dao.MessageDao;
import com.ljx.funchat.chat.domain.entity.Message;
import com.ljx.funchat.chat.domain.entity.msg.MessageExtra;
import com.ljx.funchat.chat.domain.enums.MessageTypeEnum;
import com.ljx.funchat.chat.domain.vo.req.TextMsgReq;
import com.ljx.funchat.chat.domain.vo.resp.TextMsgResp;
import com.ljx.funchat.sensitive.service.SensitiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 普通文本消息
 */
@Component
public class TextMsgHandler extends AbstractMsgHandler<TextMsgReq> {

    @Autowired
    private MessageDao messageDao;

    @Autowired
    private SensitiveService sensitiveService;

    @Override
    MessageTypeEnum getMsgTypeEnum() {
        return MessageTypeEnum.TEXT;
    }

    @Override
    protected void checkMsg(TextMsgReq body, Long roomId, Long uid) {

    }

    @Override
    public void saveMsg(Message msg, TextMsgReq body) {
        // 插入文本内容
        MessageExtra extra = Optional.ofNullable(msg.getExtra()).orElse(new MessageExtra());
        // 进一步更新消息
        Message update = new Message();
        update.setId(msg.getId());
        update.setContent(sensitiveService.getFilterStr(body.getContent()));
        update.setExtra(extra);
        // 如果有回复消息
        if (Objects.nonNull(body.getReplyMsgId())){
            update.setReplyMsgId(body.getReplyMsgId());
        }
        // 判断消息url跳转
        // 艾特功能
        if (CollectionUtil.isNotEmpty(body.getAtUidList())) {
            extra.setAtUidList(body.getAtUidList());
        }
        // 更新
        messageDao.updateById(update);
    }

    @Override
    public Object showMsg(Message msg, Long uid) {
        TextMsgResp resp = new TextMsgResp();
        resp.setContent(msg.getContent());
//        resp.setUrlContentMap(Optional.ofNullable(msg.getExtra()).map(MessageExtra::getUrlContentMap).orElse(null));
        resp.setAtUidList(Optional.ofNullable(msg.getExtra()).map(MessageExtra::getAtUidList).orElse(null));
        // todo 回复消息


        return resp;
    }

    @Override
    public Object showReplyMsg(Message msg) {
        return msg.getContent();
    }

    @Override
    public String showContactMsg(Message msg) {
        return msg.getContent();
    }
}
